var wc = require("../components/winLocation.js")
import {
    initView,
    showPromptDialog,
    hidePromptDialog
} from '../views/orderDetailView.js';
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var token, loginName, openid, loginType, trans_seq, orderId, goodsAmount;
var errorObject;
$(document).on("pageinit", "#orderDetail_page", controllerInit);

function controllerInit() {
    wc.checkBrowser();
    initValue();
    initView();
    getOrderData();
    $("#trans_t").on("touchend", function() {
        if($(this).hasClass("weui-btn weui-btn_plain-primary weui-btn_plain-disabled")){
            return;
        }else{
           showPromptDialog(); 
        }
        
    });
    $("#return-yes").on("touchend",function  () {

        returnGoods();
        hidePromptDialog();
    });
}

// 请求订单
function getOrderData() {
    var oParameters = {
        "token": token,
        "bizLicNo": loginName,
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType,
        "trans_seq": trans_seq
    }

    $.post(TransDetail_URL, oParameters, odback)
}

// 订单详情回调函数
function odback(odata) {
    if (odata.login_response) {
        haslogin(odata);
        return;
    }
    console.dir(odata);

    if (odata.tltdtrans_response && odata.tltdtrans_response.trans_info_arrays && odata.tltdtrans_response.trans_info_arrays.trans_info && odata.tltdtrans_response.trans_info_arrays.trans_info[0]) {

        var trans_info = odata.tltdtrans_response.trans_info_arrays.trans_info[0];
        var format_time = changeDateTime(String(trans_info.trans_date) + String(trans_info.trans_time),"second");
        var order_id_str = String(trans_info.order_id).replace("s","");
        orderId = order_id_str;
        goodsAmount = trans_info.goods_Amount;

        $("#order-id").text(order_id_str);
        $("#trans_status").text(trans_info.trans_status);
        $("#trans_amt").text(trans_info.trans_amt);
        $("#order-time").text(format_time);
        $("#trans_type").text(trans_info.nt);
        $("#trans_t").text(trans_info.trans_type);
        $("#term_id").text(trans_info.term_id);
        $("#goods_Amount").text(trans_info.goods_Amount);
        $("#card_No").text(trans_info.credit_Card_No);
        if(trans_info.trans_type != "退货"){
            $("#trans_t").attr("class","weui-btn weui-btn_plain-primary weui-btn_plain-disabled");
        }
        $("#settlement_amt").text(trans_info.settlement_amt);
        $("#stage_count").text(trans_info.stage_count + "期");
        $("#rtngds_pay_amt").text(trans_info.rtngds_pay_amt);
    }
}

// 退货请求
function returnGoods() {
    var rgParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType,
        "orderId": orderId,
        "goodsAmount": goodsAmount
    };
    $.post(MerRefunds_URL, rgParameters, rgback);
}

//  退货回调函数
function rgback (rgdata) {
    if (rgdata.login_response) {
        haslogin(rgdata);
        return;
    }
    console.dir(rgdata);
    if(rgdata.lft_orderinfo_response && rgdata.lft_orderinfo_response.resp_code == "0000"){
        errorObject.showErroDialog("退货申请成功");
    }else{
        errorObject.showErroDialog("退货失败");
    }
    getOrderData();
}


// 时间20161108160023 转为 2016-11-08 16:00:23
function changeDateTime(dateString, type) {
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}

// 初始化变量
function initValue() {
    wc.QueryString.Initial();
    trans_seq = wc.QueryString.GetValue("trans_seq");
    getSessionStorage();
    errorObject = new errorDialog();
    logoutObject = new Logout();
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}